<template>
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="560px"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="first">
        <el-form
          :model="EntryModel"
          :rules="formDietary"
          ref="EntryForm"
          label-width="110px"
          label-position="right"
          size="small"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="编码" prop="coding">
                <el-input v-model="EntryModel.coding" ref="coding"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="项目名称" prop="name">
                <el-input v-model="EntryModel.name" ref="name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="助记名" prop="helpname">
                <el-input v-model="EntryModel.helpname"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="适用性别" prop="sex">
                <el-select v-model="EntryModel.sex">
                  <el-option
                    ref="sex"
                    v-for="item in sex"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="所属分类" prop="category">
                <el-input
                  v-model="EntryModel.category"
                  ref="category"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="项目类型" prop="genre">
                <el-input v-model="EntryModel.genre" ref="genre"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="价格" prop="price">
                <el-input v-model="EntryModel.price" ref="price"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="成本价" prop="costprice">
                <el-input
                  v-model="EntryModel.costprice"
                  ref="costprice"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="项目组" prop="tId">
                <el-select v-model="EntryModel.tId">
                  <el-option
                    v-for="item in eteam"
                    :key="item.eid"
                    :label="item.tName"
                    :value="item.eid"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="项目说明" prop="expository">
            <el-input
              type="textarea"
              resize="none"
              v-model="EntryModel.expository"
            ></el-input>
          </el-form-item>

          <el-form-item label="注意事项" prop="matters">
            <el-input
              type="textarea"
              resize="none"
              v-model="EntryModel.matters"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submitForm('EntryForm')"
        >保 存</el-button
      >
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addEntry, editEntry, selectTeamsAll } from "@/api/Appointment/entry";

export default {
  name: "role-edit",
  data() {
    return {
      activeName: "first",

      sex: [
        { value: "0", label: "不限" },
        { value: "1", label: "男" },
        { value: "2", label: "女" }
      ],

      eteam: [],

      EntryModel: [
        {
          id: "",
          tId: "",
          coding: "",
          name: "",
          genre: "",
          category: "",
          sex: "",
          price: "",
          expository: "",
          helpname: "",
          costprice: "",
          matters: ""
        }
      ],

      //加载
      locked: false,

      dialogProps: {
        visible: false,
        action: "",
        title: ""
      },
      formDietary: {
        coding: [{ required: true, message: "请输入编码", trigger: "blur" }],
        name: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],

        category: [
          { required: true, message: "请输入所属分类", trigger: "blur" }
        ],
        genre: [{ required: true, message: "请输入项目类型", trigger: "blur" }],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        costprice: [
          { required: true, message: "请输入成本价", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //选项卡点击
    handleClick(tab, event) {
      console.log(tab, event);
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveEntry();
          } else {
            this.doUpdateEntry();
          }
        } else {
          return false;
        }
      });
    },
    doUpdateEntry() {
      editEntry(this.EntryModel)
        .then(response => {
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "修改成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("修改失败");
        });
    },
    doSaveEntry() {
      addEntry(this.EntryModel)
        .then(response => {
          this.locked = false;
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "添加成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("添加失败");
        });
    },

    handleDialogClose() {
      this.dialogProps.visible = false;
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error"
      });
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on("openEditDietaryDialog", function(eModel) {
        this.EntryModel = eModel;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "编辑";
        this.dialogProps.visible = true;
      });
      this.$on("openAddDietaryDialog", function() {
        this.dialogProps.action = "add";
        this.dialogProps.title = "新增";
        this.EntryModel = {};
        this.dialogProps.visible = true;
      });
    });
  },
  created() {
    selectTeamsAll().then(response => {
      this.eteam = response.data
    });
  }
};
</script>

